﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SXICE.AdminAPI.Filters;
using SXICE.AdminAPI.Models;
using SXICE.AdminAPI.Utilities;
using SXICE.Core.Domains;
using SXICE.Core.Helpers;
using SXICE.Core.Models;
using SXICE.Core.Models.DTOs;
using SXICE.Core.Models.Search;
using SXICE.IService;
using SXICE.IService.Models;
using System.Collections.Generic;

namespace SXICE.AdminAPI.Controllers
{
    /// <summary>
    /// 权限控制器
    /// </summary>

    [Route("")]
    [Authorize]
    public class RightsController : ControllerBase
    {
        private readonly IRightsService rightsService;

        public RightsController(IRightsService rightsService)
        {
            this.rightsService = rightsService;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Rights("ViewRole")]
        public ReturnResult<List<Roles>> GetRoles()
        {
            return rightsService.GetRoles();
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="name">要添加的角色名称</param>
        /// <returns>结果</returns>
        [HttpPost]
        [Rights("AddRole")]
        public ReturnResult AddRole([FromBody] string name)
        {
            return rightsService.AddRole(name);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">要删除的角色</param>
        /// <returns>结果</returns>
        [HttpDelete]
        [Rights("DeleteRole")]
        public ReturnResult DeleteRole(int id)
        {
            return rightsService.DeleteRole(id);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roles">要修改的角色</param>
        /// <returns>结果</returns>
        [HttpPut]
        [Rights("EditRole")]
        public ReturnResult UpdateRole([FromBody] Roles roles)
        {
            return rightsService.UpdateRole(roles);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns>结果</returns>
        [HttpGet]
        public ReturnResult<List<Menu>> GetMenus()
        {
            return rightsService.GetMenus();
        }

        /// <summary>
        /// 获取当前登录人的菜单
        /// </summary>
        /// <returns>结果</returns>
        [HttpGet]
        public ReturnResult<List<Menu>> GetCurrentMenus()
        {
            return rightsService.GetMenusByRoleId(WorkContext.DoctorClaims.RoleId);
        }

        /// <summary>
        /// 根据角色ID获取权限列表DTO
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>结果</returns>
        [HttpGet]
        [Rights("AssignRoleRights")]
        public ReturnResult<List<RightsDTO>> GetRightsDTO(int roleId)
        {
            return rightsService.GetRightsDTO(roleId);
        }

        /// <summary>
        /// 根据角色ID获取权限列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>结果</returns>
        [HttpGet]
        [Rights("AssignRoleRights")]
        public ReturnResult<List<Rights>> GetRights(int roleId)
        {
            return rightsService.GetRights(roleId);
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuIds">菜单ID</param>
        /// <returns>结果</returns>
        [HttpPost]
        [Rights("AssignRoleRights")]
        public ReturnResult AddRights([FromBody] List<int> menuIds)
        {
            if (!HttpContext.Request.Query.ContainsKey("roleId"))
                return ReturnResult.GetResult(783, "请传入角色ID");
            if (!int.TryParse(HttpContext.Request.Query["roleId"], out var roleId))
                return ReturnResult.GetResult(785, "角色ID必须为数字");
            return rightsService.AddRights(roleId, menuIds);
        }
    }
}
